package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.StringIf;
import com.macro.mall.dao.PmsKeywordDao;
import com.macro.mall.dto.KeywordsReturnResult;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.PmsKeywordMapper;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.model.PmsKeyword;
import com.macro.mall.model.PmsKeywordExample;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.service.PmsKeywordService;
import com.macro.mall.service.UmsAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
@Service
public class PmsKeywordServiceImpl implements PmsKeywordService {

    @Autowired
    private PmsKeywordMapper keywordMapper;
    @Autowired
    private UmsAdminService umsAdminService;
    @Autowired
    private PmsKeywordDao pmsKeywordDao;
    @Autowired
    private PmsProductMapper productMapper;

    @Override
    public CommonResult creatPmsKeyword(String currentUsername, PmsKeyword pmsKeyword) {
        UmsAdminDTO admin = umsAdminService.getAdminDTOByUserName(currentUsername);
        //判断关键字是否重复
        PmsKeywordExample example = new PmsKeywordExample();
        example.createCriteria().andKeywordEqualTo(pmsKeyword.getKeyword()).andStoreIdEqualTo(admin.getStoreId());
        List<PmsKeyword> pmsKeywords = keywordMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(pmsKeywords)){
            return CommonResult.failed("关键字已存在");
        }
        pmsKeyword.setStoreId(admin.getStoreId());
        int count = keywordMapper.insert(pmsKeyword);
        if(count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }

    @Override
    public List<PmsKeyword> getKeyword(String currentUsername, String keyword) {
        UmsAdminDTO admin = umsAdminService.getAdminDTOByUserName(currentUsername);
        PmsKeywordExample example = new PmsKeywordExample();
        PmsKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andStoreIdEqualTo(admin.getStoreId());
        if(keyword != null && !"".equals(keyword)){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<PmsKeyword> pmsKeywords = keywordMapper.selectByExample(example);
        List<PmsKeyword> result  = new ArrayList<>();
        if(!CollectionUtils.isEmpty(pmsKeywords)){
            return pmsKeywords;
        }else{
            return result;
        }
    }

    @Override
    public PmsKeyword getKeywordById(Long id) {
        return keywordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteKeyword(String ids) {
        List<Long> keyIds = StringIf.getList4Long(ids);
        if(!CollectionUtils.isEmpty(keyIds)){
            PmsKeywordExample example = new PmsKeywordExample();
            example.createCriteria().andIdIn(keyIds);
            return keywordMapper.deleteByExample(example);
        }else{
            return 0;
        }
    }

    @Override
    public CommonResult updatePmsKeyword(String currentUsername, PmsKeyword pmsKeyword) {
        UmsAdminDTO admin = umsAdminService.getAdminDTOByUserName(currentUsername);
        PmsKeyword key = keywordMapper.selectByPrimaryKey(pmsKeyword.getId());
        if(admin.getStoreId().equals(key.getStoreId())){
            int count = keywordMapper.updateByPrimaryKey(pmsKeyword);
            if(count > 0){
                return CommonResult.success(count);
            }else {
                return CommonResult.failed();
            }
        }else{
            return CommonResult.failed("不能修改非自己门店的关键字");
        }
    }
}
